package com.hkd.user_api.controller.base;

import com.hkd.base_service.api.AreaService;
import com.hkd.base_service.api.dto.AreaUploadDTO;
import com.hkd.common.api.ApiResponse;
import com.hkd.user_api.controller.BaseController;
import com.hkd.user_api.strategy.upload.AreaUpload;
import com.hkd.user_api.strategy.upload.UploadContext;
import com.hkd.user_service.api.SysUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 初始化数据前端控制器
 *
 * @author HKD
 * @date 2022/8/20 19:33
 */
@RestController
@RequestMapping("init")
@Api(tags = "初始化")
public class InitController extends BaseController {
    @Autowired
    private SysUserService userService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private AreaUpload areaUpload;

    /**
     * 初始化系统用户
     *
     * @return
     */
    @GetMapping("initSysUser")
    @ApiOperation("初始化系统用户")
    public ApiResponse initSysUser() {
        ApiResponse response = userService.initSysUser();
        return response;
    }

    @PostMapping("initArea")
    @ApiOperation("初始化省市区")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file", required = true,dataType = "__file", value = "省市区excel表,第一列为省市区名,第二列为省市区编码")
    })
    public ApiResponse initArea(MultipartFile file) {
        List<AreaUploadDTO> areaUploadDTOS = new UploadContext<>(areaUpload).handler(file);
        areaService.uploadArea(curUserId(), areaUploadDTOS);
        return ApiResponse.ok();
    }

}
